package com.sfzd5.exam.helplibrary.taskstatisitcs;

import com.google.gson.Gson;
import com.sfzd5.modle.*;
import com.sfzd5.exam.helplibrary.exam.Answer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/*
* 所有变量赋值过程
* 1. 初始化taskModel, classId
* 2. 下载 questionList
* 3. 下载学生列表
* 4. 下载添加 答案列表
*
* */
public class TaskStatistics {
    ExamQuestionsInfo task;

    //最大的答案ID，用来获取更新时使用
    public long taskSubmitBeginId = 0;

    //习题列表 记录学生和习题信息，主要在列表中用到
    List<Question> questionList = new ArrayList<>();
    public List<QuestionInfo> questionInfoList = new ArrayList<>();
    public HashMap<Long, QuestionInfo> questionInfoHashMap = new HashMap<>();

    //学生列表
    public List<StuInfo> stuInfoList = new ArrayList<>();
    public HashMap<Long, StuInfo> stuInfoHashMap = new HashMap<>();

    public List<Tasksubmit> getTasksubmitList() {
        return tasksubmitList;
    }

    //学生提交的答案列表
    List<Tasksubmit> tasksubmitList = new ArrayList<>();

    public TaskStatistics(ExamQuestionsInfo task){
        this.task = task;
    }

    public void addTasksubmits(List<Tasksubmit> list){
        //统计每个学生的完成数量和正确数量，统计每道习题的的完成数量和正确数量
        for(Tasksubmit t : list) {
            if(questionInfoHashMap.containsKey(t.question_id)) {
                StuInfo stuInfo;
                if(stuInfoHashMap.containsKey(t.student_id)){
                    stuInfo = stuInfoHashMap.get(t.student_id);
                } else {
                    stuInfo = new StuInfo();
                    stuInfo.name = String.valueOf(t.id);
                    stuInfo.id = t.id;
                    stuInfoList.add(stuInfo);
                    stuInfoHashMap.put(stuInfo.id, stuInfo);
                }
                t.task_id = task.id;

                tasksubmitList.add(t);

                QuestionInfo questionInfo = questionInfoHashMap.get(t.question_id);

                if(questionInfo.q.questiontype.indexOf("选")>=0){
                    stuInfo.overCount++;
                    questionInfo.overCount++;

                    if(t.id>taskSubmitBeginId)
                        taskSubmitBeginId = t.id;

                    if (t.answer.equals(questionInfo.answer)) {
                        stuInfo.rightCount++;
                        questionInfo.rightCount++;
                    }
                } else {
                    stuInfo.otherCount++;
                    questionInfo.otherCount++;
                }

            }
        }
        Collections.sort(questionInfoList, comparatorQuestionInfo);
    }

    private Comparator<QuestionInfo> comparatorQuestionInfo = new Comparator<QuestionInfo>() {
        @Override
        public int compare(QuestionInfo o1, QuestionInfo o2) {
            return Float.compare(o1.getRate(), o2.getRate());
        }
    };

    public int getQuestionCount(){
        return questionList.size();
    }

    public void addQuestions(List<Question> result) {
        if(result!=null) {
            questionList.addAll(result);
            for (Question q : result) {
                //检测是不是  在线客观题  图片  提交类型
                //若为图片题，则需要显示未批阅的数量；目前全部都是在线客观题
                QuestionInfo questionInfo = new QuestionInfo();
                //questionInfo.content = q.question_text;
                questionInfo.difficult = q.difficult;
                questionInfo.q = q;
                Answer answer = new Gson().fromJson(q.answer, Answer.class);
                questionInfo.answer = answer.content;
                questionInfoHashMap.put(q.id, questionInfo);
                questionInfoList.add(questionInfo);

            }
        }
    }

    public TotalInfo getTotalInfo(){
        TotalInfo totalInfo = new TotalInfo();
        int questionCount = getQuestionCount();
        //为了计算得分情况,还需要为每道题设定分值,但为了简便期间,每题计1分
        //全局情况统计
        totalInfo.totalAllQuestion = stuInfoHashMap.size() * questionCount;
        totalInfo.totalReply = tasksubmitList.size();
        for(StuInfo stuInfo : stuInfoList){
            if(stuInfo.overCount>0) {
                if (stuInfo.getGradeByOver() == Grade.满分) {
                    totalInfo.total满分++;
                } else if (stuInfo.getGradeByOver() == Grade.优秀) {
                    totalInfo.total优秀++;
                } else if (stuInfo.getGradeByOver() == Grade.及格) {
                    totalInfo.total及格++;
                } else {
                    totalInfo.total不及格++;
                }
            }
        }
        return totalInfo;
    }
}
